package com.example.tuomintest.config;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;

/**
 * ClassicConverter : 经典转换器
 *
 * */

public class StingtiLogConverter  extends ClassicConverter {

    @Override
    public String convert(ILoggingEvent event) {
        String message = event.getFormattedMessage();
         //String message = getContext().getConfigurationLock().toString();
        return maskSensitiveData(message);
    }

    private String maskSensitiveData(String message) {
        if (message == null || message.isEmpty()) {
            return message;
        }
        //名字脱敏

        //账号脱敏
        // 手机号脱敏: 13812345678 -> 138****5678
        message = message.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");

        // 身份证号脱敏: 110105199003073335 -> 110105********3335
        message = message.replaceAll("(\\d{6})\\d{8}(\\w{4})", "$1********$2");

        // 银行卡号脱敏: 6225880137706868 -> 622588******6868
        message = message.replaceAll("(\\d{6})\\d{6}(\\d{4})", "$1******$2");

        return message;
    }

}
